<?php
/*
Plugin Name: Big Data Analytics
Plugin URI: https://8.138.188.27:8888/
Description: Advanced analytics and tracking system for WordPress
Version: 1.0.0
Author: Hefeiyu
Author URI: http://your-site.com
License: GPL2
Text Domain: big-data-analytics
*/

defined('ABSPATH') || exit;

// 定义插件常量
define('BDA_VERSION', '1.0.0');
define('BDA_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('BDA_PLUGIN_URL', plugin_dir_url(__FILE__));

// 注册激活/停用钩子（直接包含必要文件）
register_activation_hook(__FILE__, function() {
    require_once BDA_PLUGIN_DIR . 'includes/class-database.php';
    BDA_Database::install_tables();
});
register_deactivation_hook(__FILE__, function() {
    require_once BDA_PLUGIN_DIR . 'includes/class-database.php';
    BDA_Database::uninstall_tables();
});

// 通过plugins_loaded钩子初始化插件
add_action('plugins_loaded', 'bda_init_plugin');

function bda_init_plugin() {
    // 加载翻译文件（延迟到init之后）
    add_action('init', function() {
        load_plugin_textdomain('big-data-analytics', false, dirname(plugin_basename(__FILE__)) . '/languages/');
    }, 5);
    
    // 检查核心依赖
    if (!class_exists('wpdb')) {
        add_action('admin_notices', function() {
            echo '<div class="error"><p>Big Data Analytics requires WordPress to function.</p></div>';
        });
        return;
    }

    // 检查WooCommerce是否激活
    if (!class_exists('WooCommerce')) {
        add_action('admin_notices', function() {
            echo '<div class="error"><p>Big Data Analytics 需要 WooCommerce 插件支持。</p></div>';
        });
        return;
    }
    
    // 加载依赖文件
    require_once BDA_PLUGIN_DIR . 'includes/class-database.php';
    require_once BDA_PLUGIN_DIR . 'includes/class-tracker.php';
    require_once BDA_PLUGIN_DIR . 'includes/class-analytics.php';
    require_once BDA_PLUGIN_DIR . 'includes/class-api.php';
    require_once BDA_PLUGIN_DIR . 'includes/class-visualization.php';

    // 确保所有类可用
    if (class_exists('BDA_Database') && 
        class_exists('BDA_Tracker') && 
        class_exists('BDA_Analytics') && 
        class_exists('BDA_API') && 
        class_exists('BDA_Visualization')) {
        new Big_Data_Analytics();
    } else {
        add_action('admin_notices', function() {
            echo '<div class="error"><p>Plugin components failed to load. Check file permissions.</p></div>';
        });
    }
}

// 主插件类
class Big_Data_Analytics {
    public function __construct() {
        // 初始化模块
        BDA_Database::init();
        BDA_Tracker::init();
        BDA_Analytics::init();
        BDA_API::init();
        BDA_Visualization::init();

        // 注册简码
        add_shortcode('bdadashboard', [$this, 'render_analytics_shortcode']);
    }

    // 简码渲染方法
    public function render_analytics_shortcode($atts) {
        return BDA_Visualization::render_dashboard();
    }
}